package org.jbit.news.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jbit.news.dao.BaseDao;
import org.jbit.news.dao.NewsDao;
import org.jbit.news.entity.News;
import org.jbit.news.util.Page;


public class NewsDaoImpl extends BaseDao implements NewsDao{

	public List<News> getAllnews() {
		String sql="select * from news,topic where news.ntid=topic.tid order by ncreateDate desc";
		ResultSet rs=this.exxcuteQuery(sql);
		List<News> list=new ArrayList<News>();
		try {
			while(rs.next()){
				News news = new News();
				news.setNid(String.valueOf(rs.getObject("nid")));
				news.setNtid(String.valueOf(rs.getObject("ntid")));
				news.setNtitle(String.valueOf(rs.getObject("ntitle")));
				news.setNauthor(String.valueOf(rs.getObject("nauthor")));
				news.setNcreatedate(String.valueOf(rs.getObject("ncreateDate")));
				news.setNpicpath(String.valueOf(rs.getObject("npicPath")));
				news.setNcontent(String.valueOf(rs.getObject("ncontent")));
				news.setNmodifydate(String.valueOf(rs.getObject("nmodifyDate")));
				news.setNsummary(String.valueOf(rs.getObject("nsummary")));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
		
		
	}

	public List<News> getAllNewsByTID(int tid) {
		ResultSet rs=null;
		String sql ="select * from news where ntid=?";
		rs =this.exxcuteQuery(sql, tid);
		List<News> list =new ArrayList<News>();
		try {
			while (rs.next()) {
				News news=new News();
				news.setNid(String.valueOf(rs.getObject("nid")));
				news.setNtid(String.valueOf(rs.getObject("ntid")));
				news.setNtitle(String.valueOf(rs.getObject("ntitle")));
				news.setNauthor(String.valueOf(rs.getObject("nauthor")));
				news.setNcreatedate(String.valueOf(rs.getObject("ncreateDate")));
				news.setNpicpath(String.valueOf(rs.getObject("npicPath")));
				news.setNcontent(String.valueOf(rs.getObject("ncontent")));
				news.setNmodifydate(String.valueOf(rs.getObject("nmodifyDate")));
				news.setNsummary(String.valueOf(rs.getObject("nsummary")));
				list.add(news);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			this.closeAll(connection, pstmt, rs);
		}
		return list;
	}

	public int saveNews(News news) {
		String sql="insert into news(ntid,ntitle,nauthor,ncreateDate,npicPath,ncontent,nmodifyDate,nsummary) values(?,?,?,?,?,?,?,?)";
		Object[] objects={news.getNtid(),news.getNtitle(),news.getNauthor(),news.getNcreatedate(),news.getNpicpath(),news.getNcontent(),news.getNmodifydate(),news.getNsummary()};
		int num=this.executeUpdate(sql, objects);
		return num;
	}

	public int delNews(News news) {
		String sql="delete from news where nid = ?";
		int num=this.executeUpdate(sql, Integer.parseInt(news.getNid().trim()));
		return num;
	}
	//根据nid获取新闻内容
	public List<News> getNewsByNid(int nid) {
		String sql="select * from news where nid = ? order by ncreatedate desc";
		ResultSet rs=this.exxcuteQuery(sql, nid);
		List<News> list = new ArrayList<News>();
		try {
			while(rs.next()){
				News news = new News();
				news.setNid(String.valueOf(rs.getObject("nid")));
				news.setNtid(String.valueOf(rs.getObject("ntid")));
				news.setNtitle(String.valueOf(rs.getObject("ntitle")));
				news.setNauthor(String.valueOf(rs.getObject("nauthor")));
				news.setNcreatedate(String.valueOf(rs.getObject("ncreateDate")));
				news.setNpicpath(String.valueOf(rs.getObject("npicPath")));
				news.setNcontent(String.valueOf(rs.getObject("ncontent")));
				news.setNmodifydate(String.valueOf(rs.getObject("nmodifyDate")));
				news.setNsummary(String.valueOf(rs.getObject("nsummary")));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	public int updateNewsByNid(News news,int nid) {
		String sql="update news set ntid=?,ntitle=?,nauthor=?,ncontent=?,nsummary=? where nid=?";
		Object[] objects={news.getNtid(),news.getNtitle(),news.getNauthor(),news.getNcontent(),news.getNsummary(),nid};
		int num=this.executeUpdate(sql, objects);
		return num;
	}

	public List<News> getNewsByTopicTid(int ntid) {
		String sql="select * from news where ntid = ? order by ncreatedate desc limit 5";
		ResultSet rs=this.exxcuteQuery(sql, ntid);
		List<News> list = new ArrayList<News>();
		try {
			while(rs.next()){
				News news = new News();
				news.setNid(String.valueOf(rs.getObject("nid")));
				news.setNtid(String.valueOf(rs.getObject("ntid")));
				news.setNtitle(String.valueOf(rs.getObject("ntitle")));
				news.setNauthor(String.valueOf(rs.getObject("nauthor")));
				news.setNcreatedate(String.valueOf(rs.getObject("ncreateDate")));
				news.setNpicpath(String.valueOf(rs.getObject("npicPath")));
				news.setNcontent(String.valueOf(rs.getObject("ncontent")));
				news.setNmodifydate(String.valueOf(rs.getObject("nmodifyDate")));
				news.setNsummary(String.valueOf(rs.getObject("nsummary")));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}


	public int getTotalCountByNtid(int ntid) {
		String sql="select count(1) from news where ntid=?";
		ResultSet rs=this.exxcuteQuery(sql,ntid);
		int totalCount=-1;
		try {
			while(rs.next()){
				totalCount=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return totalCount;
	}

	public List<News> getPageNewsList(int ntid, int pageNo, int pageSize) {
		String sql="select * from news where ntid=?  order by ncreatedate desc limit ?,?";
		ResultSet rs=this.exxcuteQuery(sql,ntid, (pageNo-1)*pageSize,pageSize);
		List<News> list = new ArrayList<News>();
		try {
			while(rs.next()){
				News news = new News();
				news.setNid(String.valueOf(rs.getObject("nid")));
				news.setNtid(String.valueOf(rs.getObject("ntid")));
				news.setNtitle(String.valueOf(rs.getObject("ntitle")));
				news.setNauthor(String.valueOf(rs.getObject("nauthor")));
				news.setNcreatedate(String.valueOf(rs.getObject("ncreateDate")));
				news.setNpicpath(String.valueOf(rs.getObject("npicPath")));
				news.setNcontent(String.valueOf(rs.getObject("ncontent")));
				news.setNmodifydate(String.valueOf(rs.getObject("nmodifyDate")));
				news.setNsummary(String.valueOf(rs.getObject("nsummary")));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}
//	public static void main(String[] args) {
//		NewsDaoImpl ndi=new NewsDaoImpl();
//		int totc=ndi.getTotalCount();
//		Page page=new Page();
//		page.setPageSize(3);
//		page.setCurrPageNo(1);
//		page.setTotalCount(totc);
//		List<News> list=ndi.getPageNewsList(page.getCurrPageNo(), page.getPageSize());
//		System.out.println(page.getPageSize());
//		System.out.println(page.getTotalCount());
//		System.out.println(page.getTotalPageCount());
//		for (News news : list) {
//			System.out.println(news.getNtitle());
//		}
//		
//	}

	public int getNewsCountByTID(int tid) {
		ResultSet rs =null;
		String sql="select count(ntid) from news where ntid=？";
		int count =-1;
		rs=this.exxcuteQuery(sql,tid);
		try {
			while (rs.next()) {
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(connection, null, rs);
			
		}
		return count;
	}

	

	public int getTotalCount() {
		String sql="select count(1) from news ";
		ResultSet rs=this.exxcuteQuery(sql);
		int totalCount=-1;
		try {
			while(rs.next()){
				totalCount=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return totalCount;
	}

	
}
